﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using LearnNet.BLL;
using LearnNet.Common;
using LearnNet.DAL;
using LearnNet.Entity;
namespace BBSReport
{
    public partial class frmAddSite : Form
    {
        private bool isAdd = true;
        public frmAddSite()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbUrl.Text) || string.IsNullOrEmpty(tbSiteName.Text))
            {
                MessageBox.Show("请填写完整");
                return;
            }

            SiteInfoEntity siteInfo = new SiteInfoEntity();
            siteInfo.SiteName = tbUrl.Text;
            siteInfo.SiteUrl = tbSiteName.Text;
            if (checkBox1.Checked)
            {
                siteInfo.Enable = 1;
            }
            else
            {
                siteInfo.Enable = 0;
            }
            if (isAdd)
            {
                siteInfo.DataTable_Action_ = DataTable_Action.Insert;
                if (!string.IsNullOrEmpty(BLLHelper.SiteInfoDisp(tbSiteName.Text).SiteUrl))
                {
                    MessageBox.Show("此域名已经存在");
                    return;
                }
                if (BLLHelper.SiteInfoInsertUpdateDelete(siteInfo) > 0)
                {

                }
                else
                {
                    MessageBox.Show("添加失败");
                }
            }
            else
            {
                siteInfo.DataTable_Action_ = DataTable_Action.Update;
                siteInfo.Id = Utils.StringToInt(tbId.Text, 0);
                BLLHelper.SiteInfoInsertUpdateDelete(siteInfo);
                MessageBox.Show("更新成功");
                isAdd = true;
                button1.Text = "新增";
            }
            tbSiteName.Text = "";
            tbUrl.Text = "";
            tbId.Text = "";
            Bind();
        }
        protected void Bind()
        {
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1000;
            int recordNum = 0;
          List<SiteInfoEntity>  SiteInfos = BLLHelper.SiteInfoList(qp, out recordNum);
            SiteInfoEntity siteInfo2 = new SiteInfoEntity();
            siteInfo2.Id = 0;
            siteInfo2.SiteUrl = "共用内容";
            SiteInfos.Insert(0, siteInfo2);
            comboBox1.DataSource = SiteInfos;
            comboBox1.DisplayMember = "SiteUrl";
            comboBox1.ValueMember = "Id";

            //List<SiteInfoEntity> list = new List<SiteInfoEntity>();
            //QueryParam qp = new QueryParam();
            //qp.PageIndex = 1;
            //qp.PageSize = 1000;
            //int recordNum = 0;
            //list = BLLHelper.SiteInfoList(qp, out recordNum);
            dataGridView1.Rows.Clear();
            // dataGridView1.DataSource = list;
            //dataGridView1.DataMember = "id,SiteUrl,SiteName";
            dataGridView1.ColumnCount = 4;

            dataGridView1.Columns[0].Name = "Id";
            dataGridView1.Columns[0].HeaderText = "id";
            dataGridView1.Columns[1].Name = "SiteUrl";
            dataGridView1.Columns[2].Name = "SiteName";
            dataGridView1.Columns[3].Name = "Enable";
            foreach (SiteInfoEntity siteInfo in SiteInfos)
            {
                int index = dataGridView1.Rows.Add();
                DataGridViewRow dr = dataGridView1.Rows[index];

                dr.Cells[0].Value = siteInfo.Id;
                dr.Cells[1].Value = siteInfo.SiteUrl;
                dr.Cells[2].Value = siteInfo.SiteName;
                dr.Cells[3].Value = siteInfo.Enable;
               // dataGridView1.Rows.Add(dr);
            }

        }

        private void frmAddSite_Load(object sender, EventArgs e)
        {
            Bind();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowIndex = dataGridView1.CurrentCell.RowIndex; //获得行号
            if(string.IsNullOrEmpty( dataGridView1[1, rowIndex].Value.ToString())){
                return ;
            }
            if (MessageBox.Show("确定要删除" + dataGridView1[1, rowIndex].Value.ToString(), "删除确定", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
            {
                try
                {
                    SiteInfoEntity siteInfo = new SiteInfoEntity();
                    siteInfo.Id = Utils.StringToInt(dataGridView1[0, rowIndex].Value.ToString(), 0);
                    siteInfo.DataTable_Action_ = DataTable_Action.Delete;


                    BLLHelper.SiteInfoInsertUpdateDelete(siteInfo);
                    Bind();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            isAdd = true;
        }

        private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            isAdd = false;
            int rowIndex = dataGridView1.CurrentCell.RowIndex; //获得行号
            tbId.Text = dataGridView1[0, rowIndex].Value.ToString();
            tbSiteName.Text = dataGridView1[1, rowIndex].Value.ToString();
            tbUrl.Text = dataGridView1[2, rowIndex].Value.ToString();
            if (dataGridView1[3, rowIndex].Value.ToString() == "1")
            {
                checkBox1.Checked = true;
            }
            else
            {
                checkBox1.Checked = false;
            }
            button1.Text = "保存";

        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Bind();
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            if (tbId.Text == "")
            {
                MessageBox.Show("请选择要复制的网站");
                return;
            }
            if (MessageBox.Show("确认", "是否要复制"+comboBox1.SelectedValue.ToString()+"的内容", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                QueryParam qp = new QueryParam();
                qp.PageIndex = 0;
                qp.PageSize = 1000;
                qp.Where = " type=" + comboBox1.SelectedValue.ToString();
                int recordNum=0;
                List<ReportContentEntity> conents = BLLHelper.ReportContentList(qp, out recordNum);
                int addNum = 0;
                foreach (ReportContentEntity content in conents)
                {
                    if (string.IsNullOrEmpty( BLLHelper.ReportContentDisp(content.SendContent).SendContent))
                    {
                        
                        content.DataTable_Action_ = DataTable_Action.Insert;
                        content.Type = tbId.Text;
                        BLLHelper.ReportContentInsertUpdateDelete(content);
                        addNum++;
                    }
                }
                MessageBox.Show("共增加" + addNum.ToString() + "条");
                
            }
        }

   
    }
}
